<?php
  include_once('session.php');  
  include_once('db.php');
  
  //test query http://localhost/onmotion/savePoint.php?lat=48.1173&lng=-11.516666&altitude=99&name=NameTest&comment=Comment%20Test&time=123519&track=0&type=0&userLogin=SergioEstevao&userTid=1234567
  
  if ( !IsSessionValid()){    		
		// Read the input from stdin     
    // print_r(" [".file_get_contents("php://input")."]");
    echo "-1";		
		exit;
	}
	
	if ( $sessionUserPrivileges < 1) {
	  echo "-1";
    exit;
  }
    
  if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
  {    
    $lat = $_POST['lat'];
    $lng = $_POST['lng'];
    $altitude = $_POST['altitude'];
    $name = $_POST['name'];
    $comment = $_POST['comment'];
    $time = $_POST['time'];
    $track = $_POST['track'];
    $type = $_POST['type'];    
  } else {
    $lat = $_GET['lat'];
    $lng = $_GET['lng'];
    $altitude = $_GET['altitude'];
    $name = $_GET['name'];
    $comment = $_GET['comment'];
    $time = $_GET['time'];
    $track = $_GET['track'];
    $type = $_GET['type'];    
  }

  $timeParsed = mktime(substr($time,0,2),substr($time,2,2),substr($time,4,2),substr($time,8,2),substr($time,6,2),substr($time,10,4));  
  /*
  $nLat = 0.0 + substr($lat,0,2) + (substr($lat,2,7) / 60.0);
  $nLng = 0.0 + substr($lng,0,3) + (substr($lng,3,7) / 60.0);
  
  if ( substr($lat,10,1) === 'S' )
  {
    $nLat = -$nLat;
  }
  if ( substr($lng,11,1) === 'W' )
  {
    $nLng = -$nLng;
  }
  */
     
  $conn = &ADONewConnection($dbType);
  $conn->PConnect($dbServer, $dbUser, $dbPassword, $dbName);
  //$conn->debug = true;
  
  if ( $sessionUserCurrentPosition != 0){
      // there is already a point to save current position
      $query = "update point set lat=$lat, lng=$lng, altitude=$altitude, name=".$conn->qstr(utf8_encode($name)).", comment=".$conn->qstr(utf8_encode($comment)).", time=".$conn->DBTimeStamp($timeParsed) ." where id_point = $sessionUserCurrentPosition";
      if ( $conn->Execute($query) ) {
        echo $sessionUserCurrentPosition;
      }
  } else {
      // there is no point to save current position, create one
      $query = "insert into point ( lat, lng, altitude, name, comment, time, track, id_user, type) values ($lat, $lng, $altitude, ".$conn->qstr(utf8_encode($name)).",".$conn->qstr(utf8_encode($comment)).",".$conn->DBTimeStamp($timeParsed) .",$track, $sessionUserId, $type)";
      if ( $conn->Execute($query) ) {
        $sessionUserCurrentPosition = $conn->Insert_ID();       
        echo $conn->Insert_ID();
        $query = "update user set lastPosition = $sessionUserCurrentPosition where id_user = $sessionUserId";
        $conn->Execute($query);  
      }  else {
        echo "-1";  
      }          
  }
    
  if ($type != 0) {// not an simple position update  
    $query = "insert into point ( lat, lng, altitude, name, comment, time, track, id_user, type) values ($lat, $lng, $altitude, ".$conn->qstr(utf8_encode($name)).",".$conn->qstr(utf8_encode($comment)).",".$conn->DBTimeStamp($timeParsed) .",$track, $sessionUserId, $type)";
    //execute query      
    if ( $conn->Execute($query) ) {
		  echo $conn->Insert_ID();	
	  }  else {
		  echo "-1";	
	  }
	}		  
?>
